package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.UpdatePasswordPost;
import com.longcai.piaojia.fragment.MineFragment;
import com.longcai.piaojia.util.MEditTextListener;
import com.longcai.piaojia.util.MyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

/**
 * Created by Administrator on 2016/8/31.
 */
public class EditPasswordActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.edit_password_submit_tv) private TextView edit_password_submit_tv;
    @BoundView(R.id.edit_password_confirm_et) private EditText edit_password_confirm_et;
    @BoundView(R.id.edit_password_new_et) private EditText edit_password_new_et;
    @BoundView(R.id.edit_password_old_et) private EditText edit_password_old_et;

    private UpdatePasswordPost updatePasswordPost = new UpdatePasswordPost(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            super.onSuccess(toast, type, s);

            Toast.makeText(EditPasswordActivity.this, toast, Toast.LENGTH_LONG).show();

            BaseApplication.BasePreferences.saveIsLogin(false);
            BaseApplication.BasePreferences.saveUserId("");
            BaseApplication.BasePreferences.saveUserAccount("");

            startActivity(new Intent(EditPasswordActivity.this, LoginActivity.class));

            BaseApplication.INSTANCE.finishActivity(MyAccountManagerActivity.class, EditPasswordActivity.class);

            MineFragment.getUserAdvert getUserAdvert = (MineFragment.getUserAdvert) getAppCallBack(MineFragment.class);

            getUserAdvert.refreshInfo();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(EditPasswordActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_password);

        setBackTrue();

        setTitleName(getResources().getString(R.string.account_manager_edit_password_text));

        edit_password_submit_tv.setOnClickListener(this);

        edit_password_new_et.addTextChangedListener(new MEditTextListener(EditPasswordActivity.this, 16, edit_password_new_et));
        edit_password_old_et.addTextChangedListener(new MEditTextListener(EditPasswordActivity.this, 16, edit_password_old_et));
        edit_password_confirm_et.addTextChangedListener(new MEditTextListener(EditPasswordActivity.this, 16, edit_password_confirm_et));

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.edit_password_submit_tv:

                String newPassword = edit_password_new_et.getText().toString().trim();
                String oldPassword = edit_password_old_et.getText().toString().trim();
                String confirmPassword = edit_password_confirm_et.getText().toString().trim();

                if (TextUtils.isEmpty(oldPassword)) {

                    MyToast.show(context, getString(R.string.edit_password_old_hint), Toast.LENGTH_LONG);

                    return;

                } else if (oldPassword.length() < 6 || oldPassword.length() > 20) {

                    MyToast.show(context, getString(R.string.warning_please_limit_password_length_text), Toast.LENGTH_LONG);

                    return;

                } else if (TextUtils.isEmpty(newPassword)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_newpassword_text), Toast.LENGTH_LONG);

                    return;

                } else if (newPassword.length() < 6 || newPassword.length() > 20) {

                    MyToast.show(context, getString(R.string.warning_please_limit_password_length_text), Toast.LENGTH_LONG);

                    return;

                } else if (TextUtils.isEmpty(confirmPassword)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_confirmpassword_text), Toast.LENGTH_LONG);

                    return;

                } else if (!newPassword.equals(confirmPassword)) {

                    MyToast.show(context, getString(R.string.warning_password_not_same_text), Toast.LENGTH_LONG);

                    return;
                }

                updatePasswordPost.user_id = BaseApplication.BasePreferences.readUserId();
                updatePasswordPost.user_pass_new = newPassword;
                updatePasswordPost.user_pass_old = oldPassword;
                updatePasswordPost.execute(EditPasswordActivity.this);

                break;

        }

    }
}
